programming4us
           
 
 
Programming

jQuery 1.3 : Working with numeric form data (part 8) - Editing shipping information

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019
12/19/2010 4:11:20 PM

Editing shipping information

The shopping cart page also has a form for shipping information. Actually, it isn't a form at all when the page loads, and without JavaScript enabled, it remains a little box tucked away on the right side of the content area, containing a link to a page where the user can edit the shipping information:

But with JavaScript available, and with the power of jQuery at our disposal, we can turn this little link into a full-fledged form. We'll do this by requesting the form from a PHP page. Typically the data populating the form would be stored in a database of some sort, but for the purpose of this demonstration, we'll just keep some static data in a PHP array.

To retrieve the form and make it appear inside the Shipping to box, we use the $.get() method inside the .click() event handler:

$(document).ready(function() {
$('#shipping-name').click(function() {
$.get('shipping.php', function(data) {
$('#shipping-name').remove();
$(data).hide().appendTo('#shipping').slideDown();
});
return false;
});
});

By testing for the absence of the server variable $_SERVER['HTTP_X_REQUESTED_WITH'] before printing most of the page, the PHP page (shipping.php) returns only a fragment of the full page, the form, when it is requested with the $.get() method.

In the callback of the $.get() method, we remove the name that was just clicked and in its place append the form and its data from shipping.php. We then add return false so that the default event for the clicked link (loading the page indicated in the href attribute) does not occur. Now the Shipping to box is an editable form:

The user can now edit the shipping information without leaving the page.

The next step is to hijack the form submission and post the edited data back to the server with jQuery. We start by serializing the data in the form and storing it in a variable. Then we post the data back to the server using shipping.php once again: postData

$(document).ready(function() {
$('shipping form').submit(function() {
var postData = $(this).serialize();
$.post('shipping.php', postData);
return false;
};
});

The jQuery Form plugin offers a more robust .serialize() method. The plugin, which can be found at http://www.malsup.com/jquery/form/, is recommended for most AJAX form submission scenarios.


It makes sense for the form to be removed at this point and for the Shipping to box to return to its original state. We can achieve this in the callback of the $.post() method that we just used:

$(document).ready(function() {
$('#shipping form').submit(function() {
var postData = $(this).serialize();
$.post('shipping.php', postData, function(data) {
$('#shipping form').remove();
$(data).appendTo('#shipping');

});
return false;
};
});

But, this will not work! The way we have it set up now, the .submit() event handler is being bound to the Shipping to form as soon as the DOM is loaded, but the form is not in the DOM until the user clicks on the Shipping to name. The event can't be bound to something that doesn't exist.

To overcome this problem, we can put the form-creation code into a function called editShipping() and the form-submission or form-removal code into a function called saveShipping(). Then we can bind the saveShipping() function in the callback of $.get(), after the form has been created. Likewise, we can bind the function both when the DOM is ready and when the Edit shipping link is re-created in the callback of $.post(): editShipping()

$(document).ready(function() {
var editShipping = function() {

$.get('shipping.php', function(data) {
$('#shipping-name').remove();
$(data).hide().appendTo('#shipping').slideDown();
$('#shipping form').submit(saveShipping);

});
return false;
};
var saveShipping = function() {

var postData = $('#shipping :input').serialize();
$.post('shipping.php', postData, function(data) {
$('#shipping form').remove();
$(data).appendTo('#shipping');
$('#shipping-name').click(editShipping);

});
return false;
};
$('#shipping-name').click(editShipping);

});

The code has formed a circular pattern of sorts, in which one function allows for the other by rebinding their respective event handlers.


Other -----------------
- The Art of SEO : Controlling Content with Cookies and Session IDs
- iPad SDK : New Graphics Functionality - We Are All Tool Users (part 5) - The Freehand Tool
- iPad SDK : New Graphics Functionality - We Are All Tool Users (part 4) - The Ellipse and Rectangle Tools
- iPad SDK : New Graphics Functionality - We Are All Tool Users (part 3) - The Line Tool
- iPad SDK : New Graphics Functionality - We Are All Tool Users (part 2) - The Pencil Tool
- iPad SDK : New Graphics Functionality - We Are All Tool Users (part 1)
- Security-As-a-[Cloud] Service : Today’s Offerings
- CSS for Mobile Browsers : CSS Sprites
- CSS for Mobile Browsers : Common Patterns (part 4)
- CSS for Mobile Browsers : Common Patterns (part 3) - Titles and Pseudoclasses
- CSS for Mobile Browsers : Common Patterns (part 2) - Rounded corners
- CSS for Mobile Browsers : Common Patterns (part 1) - Absolute and floating positions
- iPad SDK : New Graphics Functionality - The Basic Drawing Architecture
- jQuery 1.3 : Compact forms (part 6)
- jQuery 1.3 : Compact forms (part 5)
- jQuery 1.3 : Compact forms (part 4)
- jQuery 1.3 : Compact forms (part 3)
- jQuery 1.3 : Compact forms (part 2) - AJAX auto-completion
- jQuery 1.3 : Compact forms (part 1) - Placeholder text for fields
- The Art of SEO : Duplicate Content Issues (part 3)
 
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
- First look: Apple Watch

- 3 Tips for Maintaining Your Cell Phone Battery (part 1)

- 3 Tips for Maintaining Your Cell Phone Battery (part 2)
programming4us programming4us